"""
Pydantic模型 - API请求/响应验证
"""
from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field, field_validator


class AudioProcessingRequest(BaseModel):
    """音频处理请求模型"""
    n_steps: int = Field(..., ge=-24, le=24, description="音调调节步数，范围-24到24")
    
    @field_validator('n_steps')
    @classmethod
    def validate_n_steps(cls, v):
        if not isinstance(v, int):
            raise ValueError('音调步数必须是整数')
        if v < -24 or v > 24:
            raise ValueError('音调步数必须在-24到24之间')
        return v


class AudioProcessingRecordBase(BaseModel):
    """音频处理记录基础模型"""
    filename: str = Field(..., max_length=255, description="文件名")
    file_size: int = Field(..., description="文件大小（字节）")
    file_type: str = Field(..., max_length=50, description="文件类型")
    pitch_steps: int = Field(..., description="音调调节步数")
    processing_time: Optional[float] = Field(None, description="处理耗时（秒）")
    status: str = Field(default="pending", description="处理状态")


class AudioProcessingRecordCreate(AudioProcessingRecordBase):
    """创建音频处理记录"""
    pass


class AudioProcessingRecordUpdate(BaseModel):
    """更新音频处理记录"""
    processing_time: Optional[float] = None
    status: Optional[str] = None
    error_message: Optional[str] = None


class AudioProcessingRecord(AudioProcessingRecordBase):
    """音频处理记录响应模型"""
    id: int
    error_message: Optional[str] = None
    created_at: datetime
    updated_at: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class AudioProcessingResponse(BaseModel):
    """音频处理响应模型"""
    record_id: int = Field(..., description="处理记录ID")
    filename: str = Field(..., description="处理后的文件名")
    processing_time: Optional[str] = Field(None, description="处理耗时")
    status: str = Field(..., description="处理状态")
    message: str = Field(..., description="响应消息")
    file_url: str = Field(..., description="上传文件url")
    audio_info: dict = Field(..., description="音频文件信息")


class UserBase(BaseModel):
    """用户基础模型"""
    username: str = Field(..., min_length=3, max_length=50, description="用户名")
    email: str = Field(..., description="邮箱")


class UserCreate(UserBase):
    """创建用户模型"""
    pass


class UserUpdate(BaseModel):
    """更新用户模型"""
    username: Optional[str] = Field(None, min_length=3, max_length=50)
    email: Optional[str] = None
    is_active: Optional[bool] = None


class User(UserBase):
    """用户响应模型"""
    id: int
    is_active: bool
    created_at: datetime
    updated_at: Optional[datetime] = None
    
    class Config:
        from_attributes = True


class HealthCheckResponse(BaseModel):
    """健康检查响应模型"""
    status: str = Field(..., description="服务状态")
    service: str = Field(..., description="服务名称")
    version: str = Field(..., description="服务版本")
    timestamp: datetime = Field(default_factory=datetime.now, description="检查时间")


class ErrorResponse(BaseModel):
    """错误响应模型"""
    error: str = Field(..., description="错误类型")
    message: str = Field(..., description="错误消息")
    detail: Optional[str] = Field(None, description="错误详情")
